package com.faithbj.common.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.faithbj.common.model.fm.FamilyMember;
import com.faithbj.common.utils.JsonUtil;

/**
 * 核对总体情况
 * 
 * @author XiaoQuan
 * 
 */
@Entity
@Table(name = "check_total")
public class CheckTotal implements Serializable {

	private static final long serialVersionUID = -2033388008195214480L;

	/**
	 * 申请编号-受理编号统一
	 */
	private String id;
	/**
	 * 关联的区域Id
	 */
	private String regionId;
	/**
	 * 主申请人关联的家庭成员Id
	 */
	private Set<FamilyMember> familyMembers;
	/**
	 * 这个只是配合存储用，不更新到数据库
	 */
	private FamilyMember familyMember;
	/**
	 * 委托信息
	 */
	private EntrustInfo entrustInfo;
	/**
	 * 可支配收入核定起始日期
	 */
	private Date startDateApprovedIncome;
	/**
	 * 可支配收入核定截至日期
	 */
	private Date endDateApprovedIncome;
	/**
	 * 可支配收入报告总计
	 */
	private BigDecimal reportedIncomeAmount;
	/**
	 * 可支配收入核定总计
	 */
	private BigDecimal approvedIncomeAmount;
	/**
	 * 财产核定日期
	 */
	private Date dateApprovedProperty;
	/**
	 * 财产报告总计
	 */
	private BigDecimal reportedPropertyAmount;
	/**
	 * 财产核定总计
	 */
	private BigDecimal approvedPropertyAmount;
	/**
	 * 就业状态起始日期
	 */
	private Date startDateEmployStatus;
	/**
	 * 就业状态截至日期
	 */
	private Date endDateEmployStatus;
	/**
	 * 支出起始日期
	 */
	private Date startDateSpend;
	/**
	 * 支出截止日期
	 */
	private Date endDateSpend;
	/**
	 * 支出报告总计
	 */
	private BigDecimal reportedSpendAmount;
	/**
	 * 支出核定总计
	 */
	private BigDecimal approvedSpendAmount;
	/**
	 * 是否已生成批次,true生成,false没生成
	 */
	private Boolean batched;
	/**
	 * 批次号
	 */
	private String batchNum;
	/**
	 * 创建日期
	 */
	private Date createDate;
	/**
	 * 修改日期
	 */
	private Date modifyDate;
	/**
	 * 受理机构-code
	 */
	private String handleOrg;
	/**
	 * 受理时间
	 */
	private Date handleTime;
	/**
	 * 受理人
	 */
	private String handleUser;
	/**
	 * 受理状态
	 */
	private HandleStatus handleStatus;
	/**
	 * 受理备注
	 */
	private String handleMemo;
	/**
	 * json格式存放申请材料
	 */
	private String enclosure ;	
	
	@Id
	@GeneratedValue (generator = "customIncrement")
	@GenericGenerator(name = "customIncrement", strategy = "com.faithbj.common.model.CustomIncrement")
	public String getId() {
		return id;
	}
	@Column(name = "regionId", nullable = false)
	public String getRegionId() {
		return regionId;
	}
	@OneToMany(mappedBy = "checkTotal",cascade={CascadeType.ALL})
	public Set<FamilyMember> getFamilyMembers() {
		return familyMembers;
	}
	@OneToOne(cascade = { CascadeType.ALL }, optional = true)
	@JoinColumn(name = "entrust_info", nullable = true)
	public EntrustInfo getEntrustInfo() {
		return entrustInfo;
	}	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "start_date_approved_income")
	public Date getStartDateApprovedIncome() {
		return startDateApprovedIncome;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "end_date_approved_income")
	public Date getEndDateApprovedIncome() {
		return endDateApprovedIncome;
	}	
	@Column(name = "reported_income_amount", scale = 2, precision = 12)
	public BigDecimal getReportedIncomeAmount() {
		return reportedIncomeAmount;
	}	
	@Column(name = "approved_income_amount", scale = 2, precision = 12)
	public BigDecimal getApprovedIncomeAmount() {
		return approvedIncomeAmount;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_approved_property")
	public Date getDateApprovedProperty() {
		return dateApprovedProperty;
	}
	@Column(name = "reported_property_amount", scale = 2, precision = 12)
	public BigDecimal getReportedPropertyAmount() {
		return reportedPropertyAmount;
	}
	@Column(name = "approved_property_amount", scale = 2, precision = 12)
	public BigDecimal getApprovedPropertyAmount() {
		return approvedPropertyAmount;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "start_date_employ_status")
	public Date getStartDateEmployStatus() {
		return startDateEmployStatus;
	}	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "end_date_employ_status")
	public Date getEndDateEmployStatus() {
		return endDateEmployStatus;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "start_date_spend")
	public Date getStartDateSpend() {
		return startDateSpend;
	}	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "end_date_spend")
	public Date getEndDateSpend() {
		return endDateSpend;
	}
	@Column(name = "reported_spend_amount", scale = 2, precision = 12)
	public BigDecimal getReportedSpendAmount() {
		return reportedSpendAmount;
	}
	@Column(name = "approved_spend_amount", scale = 2, precision = 12)
	public BigDecimal getApprovedSpendAmount() {
		return approvedSpendAmount;
	}	
	@Column(name = "batched", nullable = true)	
	public Boolean getBatched() {
		return batched;
	}
	@Column(name = "batch_num")
	public String getBatchNum() {
		return batchNum;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "create_date")
	public Date getCreateDate() {
		return createDate;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "modify_date")
	public Date getModifyDate() {
		return modifyDate;
	}
	@Column(name = "handle_org")
	public String getHandleOrg() {
		return handleOrg;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "handle_time")
	public Date getHandleTime() {
		return handleTime;
	}
	@Column(name = "handle_user")
	public String getHandleUser() {
		return handleUser;
	}
	@Column(name = "handle_status")
	public HandleStatus getHandleStatus() {
		return handleStatus;
	}
	@Column(name = "handle_memo")
	public String getHandleMemo() {
		return handleMemo;
	}
	@Column(nullable=true, length = 5000)
	public String getEnclosure() {
		return enclosure;
	}
	

	public void setId(String id) {
		this.id = id;
	}
	public void setFamilyMembers(Set<FamilyMember> familyMembers) {
		this.familyMembers = familyMembers;
	}
	public void setStartDateApprovedIncome(Date startDateApprovedIncome) {
		this.startDateApprovedIncome = startDateApprovedIncome;
	}
	public void setEndDateApprovedIncome(Date endDateApprovedIncome) {
		this.endDateApprovedIncome = endDateApprovedIncome;
	}
	public void setReportedIncomeAmount(BigDecimal reportedIncomeAmount) {
		this.reportedIncomeAmount = reportedIncomeAmount;
	}
	public void setApprovedIncomeAmount(BigDecimal approvedIncomeAmount) {
		this.approvedIncomeAmount = approvedIncomeAmount;
	}
	public void setReportedPropertyAmount(BigDecimal reportedPropertyAmount) {
		this.reportedPropertyAmount = reportedPropertyAmount;
	}
	public void setApprovedPropertyAmount(BigDecimal approvedPropertyAmount) {
		this.approvedPropertyAmount = approvedPropertyAmount;
	}
	public void setStartDateEmployStatus(Date startDateEmployStatus) {
		this.startDateEmployStatus = startDateEmployStatus;
	}
	public void setEndDateEmployStatus(Date endDateEmployStatus) {
		this.endDateEmployStatus = endDateEmployStatus;
	}
	public void setStartDateSpend(Date startDateSpend) {
		this.startDateSpend = startDateSpend;
	}
	public void setEndDateSpend(Date endDateSpend) {
		this.endDateSpend = endDateSpend;
	}
	public void setReportedSpendAmount(BigDecimal reportedSpendAmount) {
		this.reportedSpendAmount = reportedSpendAmount;
	}
	public void setApprovedSpendAmount(BigDecimal approvedSpendAmount) {
		this.approvedSpendAmount = approvedSpendAmount;
	}
	public void setBatched(Boolean batched) {
		this.batched = batched;
	}
	public void setBatchNum(String batchNum) {
		this.batchNum = batchNum;
	}
	public void setRegionId(String regionId) {
		this.regionId = regionId;
	}
	public void setHandleOrg(String handleOrg) {
		this.handleOrg = handleOrg;
	}
	public void setHandleTime(Date handleTime) {
		this.handleTime = handleTime;
	}
	public void setHandleUser(String handleUser) {
		this.handleUser = handleUser;
	}
	public void setHandleStatus(HandleStatus handleStatus) {
		this.handleStatus = handleStatus;
	}
	public void setHandleMemo(String handleMemo) {
		this.handleMemo = handleMemo;
	}
	public void setEntrustInfo(EntrustInfo entrustInfo) {
		this.entrustInfo = entrustInfo;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}	
	public void setDateApprovedProperty(Date dateApprovedProperty) {
		this.dateApprovedProperty = dateApprovedProperty;
	}
	public void setEnclosure(String enclosure) {
		this.enclosure = enclosure;
	}	


	@Override
	public int hashCode() {
		return id == null ? System.identityHashCode(this) : id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass().getPackage() != obj.getClass().getPackage()) {
			return false;
		}
		final CheckTotal other = (CheckTotal) obj;
		if (id == null) {
			if (other.getId() != null) {
				return false;
			}
		} else if (!id.equals(other.getId())) {
			return false;
		}
		return true;
	}
	
	@Transient
	public void copyProperties(CheckTotal checkTotal) {
		setApprovedIncomeAmount(checkTotal.getApprovedIncomeAmount());
		setApprovedPropertyAmount(checkTotal.getApprovedPropertyAmount());
		setApprovedSpendAmount(checkTotal.getApprovedSpendAmount());
		setBatched(checkTotal.getBatched());
		setBatchNum(checkTotal.getBatchNum());
		setDateApprovedProperty(checkTotal.getDateApprovedProperty());
		setEndDateApprovedIncome(checkTotal.getEndDateApprovedIncome());
		setEndDateEmployStatus(checkTotal.getEndDateEmployStatus());
		setEndDateSpend(checkTotal.getEndDateSpend());
		setHandleMemo(checkTotal.getHandleMemo());
		setHandleOrg(checkTotal.getHandleOrg());
		setHandleStatus(checkTotal.getHandleStatus());
		setHandleTime(checkTotal.getHandleTime());
		setHandleUser(checkTotal.getHandleUser());
		setReportedIncomeAmount(checkTotal.getReportedIncomeAmount());
		setReportedPropertyAmount(checkTotal.getReportedPropertyAmount());
		setReportedSpendAmount(checkTotal.getReportedSpendAmount());
		setStartDateApprovedIncome(checkTotal.getStartDateApprovedIncome());
		setStartDateEmployStatus(checkTotal.getStartDateEmployStatus());
		setStartDateSpend(checkTotal.getStartDateSpend());
	}
	
/*	@Transient
	public void onSave()
	{
	}*/
	
	
	@Transient
	public FamilyMember getFamilyMember() {
		return familyMember;
	}
	@Transient
	public void setFamilyMember(FamilyMember familyMember) {
		this.familyMember = familyMember;
	}

	
	private List<Enclosure> enclosureList;
	
	@Transient
	public List<Enclosure> getEnclosureList() {
		return enclosureList;
	}
	@Transient
	public void setEnclosureList(List<Enclosure> enclosureList) {
		this.enclosureList=enclosureList;
	}
	
	
	/**
	 * 设置申请材料，以json串存储在enclosure里面
	 */
	@Transient
	public void setEnclosures() {
		if (enclosureList != null && enclosureList.size() >= 0) {
			enclosure=JsonUtil.toJson(enclosureList);
		}
	}


	/**
	 * 获取主申请人信息,把set中的值那出来,遍历找到主申请人,放到familymember中
	 */
	@Transient
	public void setMainFamilyMember() {
		for(FamilyMember fm:this.familyMembers)
		{
			if(fm.getMainApplicant())
				this.familyMember= fm;
		}
	}

}
